import { GREASY_FORK_PAGINATION_CONFIG } from '../../../../../lib/page_handler/platform/greasyfork/types'
import { HOSTNAME, SUPPORTED_PLATFORMS } from '../../../../../types/constants'
import { IPaginationLoadType } from '../index'
import { AutoPaginationBase } from './base'

export enum GreasyForkSearchParams {
  Page = 'page',
}
export interface IGreasyForkUrlParams {
  [GreasyForkSearchParams.Page]: string
}

export class AutoPaginationGreasyFork extends AutoPaginationBase {
  // @Decorators.Identity
  readonly name: string = 'AutoPaginationGreasyFork'
  // @Decorators.PublicState
  private static instance: AutoPaginationGreasyFork
  // @Decorators.Config
  readonly loadType: IPaginationLoadType = IPaginationLoadType.Pagination
  readonly defaultUrlParams: IGreasyForkUrlParams = { // 搜索链接默认参数值
    [GreasyForkSearchParams.Page]: '1',
  }

  /**
   * 获取单例实例
   */
  public static getInstance(): AutoPaginationGreasyFork {
    if (!AutoPaginationGreasyFork.instance) {
      AutoPaginationGreasyFork.instance = new AutoPaginationGreasyFork()
    }
    return AutoPaginationGreasyFork.instance
  }

  /**
   * 检查页面
   */
  checkPage(): boolean {
    return HOSTNAME === SUPPORTED_PLATFORMS.GREASY_FORK
  }

  /**
   * 从分页元素中获取最大页码数
   */
  getMaxPageNumFromPagination(): number {
    return this.clacMaxPageNum(GREASY_FORK_PAGINATION_CONFIG.selector.item)
  }
}
